
package com.zmn.oms.task.job.collectionnotpay;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.zmn.business.interfaces.collectionnotpay.CollectionNotPayBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述：收款未支付的订单，系统定时通过工程师质保金支付订单
 * 每周二的凌晨5点执行
 *
 * @author yangshuangping
 * @date 2022/07/12 17:58
 */

@LTS
@Slf4j
public class CollectionNotPayJob {

    /**
     * 添加定时任务服务
     */

    @Resource
    private TaskManager taskManager;

    @Autowired
    private CollectionNotPayBService collectionNotPayBService;


    /**
     * 自动添加定时任务，每周二的凌晨5点执行 "0 0 5 ? * 3 *"
     */

    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_COLLECTION_NOT_PAY_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_COLLECTION_NOT_PAY_JOB);
        job.setCronExpression("0 0 5 ? * 3 *");
        job.setReplaceOnExist(true);
        ResponseDTO<String> resp = taskManager.submitTask2(job);
        if (!resp.isSuccess()) {
            log.error("#oms# CollectionNotPayJob job submit fail...");
        }
    }


    /**
     * 每周二的凌晨5点执行
     *
     * @param job
     * @return
     */

    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_COLLECTION_NOT_PAY_JOB)
    public Result collectionNotPayOrder(Job job) {
        log.info("#oms#collectionNotPayOrder# 开始执行收款未支付订单自动质保金支付。");
        collectionNotPayBService.collectionNotPay();
        log.info("#oms#delete#grab#order# 结束执行收款未支付订单自动质保金支付。");
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}

